﻿using HardwareInterface;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using YoloPredictor;

namespace FruitDetectorLiveDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            testabli.Text = "0";
            CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lock (testabli) ;
            new Thread(new ThreadStart(() =>
            {
                lock (testabli)
                {
                    Camera cam = new Camera((int)numericUpDown1.Value);
                    cam.Init();
                    IPredictor pred = PredictorLib.CreateDefaultPredictor(new PredictorConfig()
                    {
                        UseCUDA = false,
                        Names = "./module/obj.names",
                        Config = "./module/detector.cfg",
                        Weights = "./module/detector.weights",
                        NmsThreshold = 0.3f,
                        Threshold = 0.5f
                    });
                    while (true)
                    {
                        var frame = cam.GetLatestFrame();
                        //double rate = 416f / (float)frame.Height;
                        //Cv2.Resize(frame, frame, new OpenCvSharp.Size(rate * frame.Height, 416), 0, 0, InterpolationFlags.Cubic);
                        var center_x = frame.Width / 2;
                        var center_y = frame.Height / 2;
                        DateTime start = DateTime.Now;
                        var predictions = pred.Predict(frame);
                        var timeused = (DateTime.Now - start).TotalSeconds;
                        testabli.Text = timeused.ToString();
                        double closestdist = double.MaxValue;
                        Cv2.Line(frame, new OpenCvSharp.Point(center_x, center_y - 5), new OpenCvSharp.Point(center_x, center_y + 5), Scalar.Green, 2);
                        Cv2.Line(frame, new OpenCvSharp.Point(center_x - 5, center_y), new OpenCvSharp.Point(center_x + 5, center_y), Scalar.Green, 2);
                        if (predictions.Count() > 0)
                        {
                            Prediction closest = predictions[0];
                            //找出最接近准心的目标
                            foreach (var p in predictions)
                            {
                                if (p.ClassName != "cucumber") continue;
                                int x = (int)(p.Box.X - center_x);
                                int y = (int)(p.Box.Y - center_y);
                                double distance = Math.Sqrt((x * x) + (y * y));
                                if (closestdist > distance)
                                {
                                    closestdist = distance;
                                    closest = p;
                                }
                                Cv2.Line(frame, new OpenCvSharp.Point(p.Box.X - 5, p.Box.Y - 5), new OpenCvSharp.Point(p.Box.X + 5, p.Box.Y + 5), Scalar.Red, 2);
                                Cv2.Line(frame, new OpenCvSharp.Point(p.Box.X - 5, p.Box.Y + 5), new OpenCvSharp.Point(p.Box.X + 5, p.Box.Y - 5), Scalar.Red, 2);
                            }
                        }
                        Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(frame);
                        pictureBox1.Image = bitmap;
                    }
                }
            })).Start();
        }
    }
}
